package cn.fl.project.service.impl;

import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjLnkCompOrgRService;
import cn.fl.project.transform.PrjLnkCompOrgRVOTransform;
import cn.fl.project.vo.PrjLnkCompOrgRVO;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompApiService;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompQueryApiService;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompOrgRQC;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompOrgRDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 挂靠公司-事业部关系表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjLnkCompOrgRServiceImpl implements IPrjLnkCompOrgRService {

	@Autowired
	private ISysOrgApiService sysOrgApiService;// 组织机构

	@Autowired
	private IPrjLnkCompQueryApiService prjLnkCompQueryApiService ;

	@Autowired
	private IPrjLnkCompApiService prjLnkCompApiService ;

	@Override
	public PrjLnkCompOrgRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//TODO 业务逻辑
		PrjLnkCompOrgRDTO dto = null;//this.自定义ApiService.selectById(id);
		return PrjLnkCompOrgRVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjLnkCompOrgRVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		List<SysOrgDTO> list = this.sysOrgApiService.searchSysOrgListBy(CurrentThreadContext.getCurrentSysCd());
        Map<String, SysOrgDTO> buOrgCdMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (SysOrgDTO sysOrgDTO : list) {
                buOrgCdMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO);
            }
        }
        if (buOrgCdMap.containsKey(entity.getBuOrgCd())) {
        	entity.setBuOrgNm(buOrgCdMap.get(entity.getBuOrgCd()).getOrgDispNm());
        }
		return this.prjLnkCompApiService.addPrjLnkCompOrgR(PrjLnkCompOrgRVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.prjLnkCompOrgRApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(PrjLnkCompOrgRVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		//return this.prjLnkCompOrgRApiService.modify(PrjLnkCompOrgRVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		//return this.prjLnkCompOrgRApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}

	@Override
	public List<PrjLnkCompOrgRVO> searchList(PrjLnkCompOrgRQC qc) throws BaseException {
		if(null == qc){
			return null;
		}
		return PrjLnkCompOrgRVOTransform.toVOList(this.prjLnkCompQueryApiService.getPrjLnkCompOrgRByCondition(qc));
	}
}
